<?php
namespace app\api\controller;
use app\BaseController;
use app\common\model\StaffModel;

use app\common\model\StaffManageModel;

class ManageStaff extends BaseController{
    public function detail(){
        $manage_code = $this->request->param('manage_code/d',0);
        if($manage_code == 0){
            return error('参数错误');
        }
        if(null === $StaffModel = StaffModel::with(['detail','profile'])->where('code',$manage_code)->find()){
            return error('参数错误');
        }
        
        $staff_manage_id = StaffManageModel::where([
            'user_id'=>$this->request->user_info->user_id,
            'staff_id' => $StaffModel->staff_id
            ])->value('staff_manage_id');
        if(!$staff_manage_id){
            return error('无权限');
        }
        
        
        return success(['info'=>$StaffModel]);
    }
    public function save(){
       $staff_id = $this->request->param('staff_id/d',0);

        $name = $this->request->param('name/s','');
        $mobile  = $this->request->param('mobile/s','');
        $position = $this->request->param('position/s','');
        // $sort = $this->request->param('sort/d',0);
        // $status = $this->request->param('status/d',1);
        // $notes = $this->request->param('notes/s','');
        
        // $bind_code = $this->request->param('bind_code/d',0);

        $name_en = $this->request->param('name_en/s','');
        $position_en = $this->request->param('position_en/s','');
        $weixin  = $this->request->param('weixin/s','');
        
        $wx_qr  = $this->request->param('wx_qr/s','');
        $mail  = $this->request->param('mail/s','');
        $spare1 = $this->request->param('spare1/s','');
        $spare2 = $this->request->param('spare2/s','');
        
        $is_card  = $this->request->param('is_card/d',0);
        $card  = $this->request->param('card/s','');
        $face    = $this->request->param('face/s','');
        // $tag = $this->request->param('tag/a',[]);
        $is_share  = $this->request->param('is_share/d',0);
        $share_text = $this->request->param('share_text/s','');
        $share_image = $this->request->param('share_image/s','');
        
        $profile_type = $this->request->param('profile_type/s','text');
        $profile_text = $this->request->param('profile_text/s','');
        
        if(empty($name)){
            return error('请填写员工姓名');
        }
        
        if(empty($staff_id)){
            return error('参数错误');
        }
        if(null === $StaffModel = StaffModel::find($staff_id)){
            return error('用户不存在');
        }
        $staff_manage_id = StaffManageModel::where([
            'user_id'=>$this->request->user_info->user_id,
            'staff_id' => $StaffModel->staff_id
            ])->value('staff_manage_id');
        if(!$staff_manage_id){
            return error('无权限');
        }
        
        $StaffModel->name = $name;
        $StaffModel->mobile = $mobile;
        $StaffModel->position = $position;
        $StaffModel->save();
        
        $StaffModel->detail->name_en = $name_en;
        $StaffModel->detail->position_en = $position_en;
        $StaffModel->detail->weixin = $weixin;
        $StaffModel->detail->wx_qr = $wx_qr;
        
        $StaffModel->detail->is_card = $is_card;
        $StaffModel->detail->card = $card;
        $StaffModel->detail->face = $face;

        $StaffModel->detail->mail = $mail;
        $StaffModel->detail->spare1 = $spare1;
        $StaffModel->detail->spare2 = $spare2;
        
   
        $StaffModel->detail->is_share = $is_share;
        $StaffModel->detail->share_image = $share_image;
        $StaffModel->detail->share_text = $share_text;
        $StaffModel->detail->save();
        
        $StaffModel->profile->profile_type = $profile_type;
        $StaffModel->profile->profile_text = $profile_text;
        $StaffModel->profile->save();
        return success('保存成功');
    }
}